package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.util.POIUtils;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author zhangmeng
 * @description 检查组控制器
 * @date 2019/9/18
 **/
@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {
    @Autowired
    private OrderSettingService orderSettingService;

    @PostMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) throws IOException {
        List<String[]> excel = POIUtils.readExcel(excelFile);
        List<OrderSetting> orderSettingList = new ArrayList<>();
        for (String[] strings : excel
        ) {
            OrderSetting orderSetting = new OrderSetting();
            orderSetting.setOrderDate(new Date(strings[0]));
            orderSetting.setNumber(Integer.parseInt(strings[1]));
            orderSettingList.add(orderSetting);
        }
        orderSettingService.insert(orderSettingList);
        return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
    }

@GetMapping("/getOrderSettingByMonth")


    public Result getOrderSettingByMonth(@RequestParam(value = "year") String year, @RequestParam(value = "month") String month){
    List<OrderSetting> orderSettingByMonth = orderSettingService.getOrderSettingByMonth(year, month);
    List<Map<String,Object>> list = new ArrayList<>();
    for (OrderSetting orderSetting:orderSettingByMonth
         ) {
        Map map = new HashMap<>();
        map.put("date",orderSetting.getOrderDate().getDate());
        map.put("number",orderSetting.getNumber());
        map.put("reservations",orderSetting.getReservations());
        list.add(map);
    }
    return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS,list);
}

@PostMapping("/editNumberByDate")
    public Result editNumberByDate (@RequestBody OrderSetting orderSetting){
        orderSettingService.updateNumberOrderDate(orderSetting);
        return new Result(true,MessageConst.EDIT_CHECKGROUP_SUCCESS);
}

}
